package com.xxx.auth.common.web.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * rest接口通用返回值数据结构
 *
 * @param <T>
 */
@Data
@Accessors(chain = true)
public class ResultDTO<T> {
    @Schema(title = "响应代码")
    private String code;
    @Schema(title = "提示信息")
    private String message;
    @Schema(title = "数据")
    private T data;

    public static <T> ResultDTO<T> success() {
        ResultDTO<T> result = new ResultDTO<>();
        result.setCode("200");
        result.setData(null);
        return result;
    }

    public static <T> ResultDTO<T> success(T data) {
        ResultDTO<T> result = new ResultDTO<>();
        result.setCode("200");
        result.setData(data);
        return result;
    }

    public static <T> ResultDTO<T> success(T data, String message) {
        ResultDTO<T> result = new ResultDTO<>();
        result.setCode("200");
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> ResultDTO<T> error() {
        return error(null, null);
    }

    public static <T> ResultDTO<T> error(String message) {
        return error(null, message);
    }

    public static <T> ResultDTO<T> error(String code, String message) {
        return error(code, message, null);
    }

    public static <T> ResultDTO<T> error(String code, String message, T data) {
        ResultDTO<T> result = new ResultDTO<>();
        result.setCode(code == null ? "500" : code);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static <T> ResultDTO<T> paramError() {
        return error("400", null);
    }
}